import os 
import sys
import json

class createNewInstitution(object):

    def __init__(self, 
                 group_name:str, 
                 database:str, 
                 group_code:str, 
                 mapping_path:str):

        """初始化类，设置组名和数据库类型"""
        self.group_name = group_name
        self.database = database
        self.group_code = group_code
        self.mapping_path = mapping_path
        self.template = self._get_template()

    def _get_template(self):
        """根据 database 类型生成对应的配置模板"""
        # 定义基础模板
        template = {
            "base_url": "",
            "group_code": self.group_code,
            "mapping_path": self.mapping_path,
            "aeskey": "dds5ADaF6O64$^45,fds",
            "database": self.database,
            "user_last_id": -1000,
            "item_last_id": -1,
            "loan_last_id": -1,
            "record_last_id": -1,
            "metadata_last_id": -1,
            "metadata_record_last_id": -1,
            "model_dir": f"../DSSM/Saved_models/{self.group_name}",
            "saved_processed_data_path": f"../DSSM/data_to_model/{self.group_name}",
            "feature_transformer_dir": f"../DSSM/Output/{self.group_name}/feature_transformer",
            "user_key2index": f"../DSSM/Output/{self.group_name}/user_key2index",
            "tag_key2index": f"../DSSM/Output/{self.group_name}/tag_key2index",
            "embedding_saved_dir": f"../Upload/Dataset/{self.group_name}/embedding",
            "user_id_to_index": f"../Upload/Dataset/{self.group_name}/user_id_to_index.json",
            "tag2recordids_path": f"../Upload/Dataset/{self.group_name}/tag2Record_ids.csv",
            "data_config": {
                "loan_data": {
                    "user_id": {"input_name": "USER_ID", "input_type": "INT32"},
                    "item_id": {"input_name": "ITEM_ID", "input_type": "INT32"},
                    "loan_date": {"input_name": "LOAN_DATE", "input_type": "datetime"}
                },
                "user_data": [
                    {"input_name": "USER_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "GENDER", "input_type": "STRING", "feature_type": "IdFeature"},
                    {"input_name": "COLLEGE_YEAR_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "COLLEGE_DEPT_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "MAJOR_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "TOTAL_LEND", "input_type": "INT32", "feature_type": "DenseFeature"},
                    {"input_name": "RESEARCHER_FLAG", "input_type": "Char", "feature_type": "IdFeature"},
                    {"input_name": "EDU_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "COLLEGE_CLASS_ID", "input_type": "INT32", "feature_type": "IdFeature"}
                ],
                "item_data": [
                    {"input_name": "ITEM_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "CUR_LOCATION_ID", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "TOTAL_CIRC_COUNT", "input_type": "INT32", "feature_type": "DenseFeature"},
                    {"input_name": "GET_MODE_CODE", "input_type": "INT32", "feature_type": "IdFeature"},
                    {"input_name": "CIRC_NUMBER", "input_type": "INT32", "feature_type": "DenseFeature"},
                    {"input_name": "tag", "input_type": "String", "feature_type": "VarFeature"}
                ],
                "input_type": "CSVInput",
                "separator": ","
            }
        }

        # 根据 database 类型设置不同的字段
        if self.database == "aliyun":
            template["base_url"] = "http://47.110.41.47:10015"
        elif self.database == "chaoxingyun":
            template["base_url"] = "http://118.186.60.40:1015"  # 可替换为实际 URL
        elif self.database == "chaoxingyun_gt":
            template["base_url"] = "http://118.186.60.40:1016"
        else:
            raise ValueError(f"不支持的 database 类型: {self.database}")

        return template

    def generate_config(self):
        """生成并保存配置文件"""
        save_dir = '/data/nvme0n1p1/opt/wyh/LSP_book_rec/configs'
        filename = f"{self.group_name}.json"
        file_path = os.path.join(save_dir, filename)
        with open(file_path, 'w', encoding='utf-8') as f:
            json.dump(self.template, f, ensure_ascii=False, indent=4)
        print(f"配置文件 {file_path} 已生成。")

# 示例运行代码
if __name__ == "__main__":
    ci = createNewInstitution(group_name="GZU", group_code="201177", database="chaoxingyun", mapping_path="gzu")
    ci.generate_config()